﻿/********************************************************************
	日期:2022/03/27
	名称:WindowRoot
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:窗口基类
*********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class WindowRoot : MonoBehaviour{
    protected ResSvc resSvc;
    protected AudioSvc audioSvc;
    protected NetSvc netSvc;
    protected TimerSvc timerSvc;

    public void SetWndState(bool isActive = true){
        if (gameObject.activeSelf != isActive){
            gameObject.SetActive(isActive);
        }

        if (isActive){
            InitWnd();
        }
        else{
            ClearWnd();
        }
    }

    public bool GetWndState(){
        return gameObject.activeSelf;
    }

    protected virtual void InitWnd(){
        resSvc = ResSvc.Instance;
        audioSvc = AudioSvc.Instance;
        netSvc = NetSvc.Instance;
        timerSvc = TimerSvc.Instance;
    }

    protected virtual void ClearWnd(){
        resSvc = null;
        audioSvc = null;
        netSvc = null;
    }

    #region Tool Function

    protected void SetActive(GameObject obj, bool isActive = true){
        obj.SetActive(isActive);
    }

    protected void SetActive(Transform trans, bool state = true){
        trans.gameObject.SetActive(state);
    }

    protected void SetActive(RectTransform trans, bool state = true){
        trans.gameObject.SetActive(state);
    }

    protected void SetActive(Image img, bool state = true){
        img.transform.gameObject.SetActive(state);
    }

    protected void SetActive(Text txt, bool state = true){
        txt.gameObject.SetActive(state);
    }

    protected void SetText(Text txt, string context = ""){
        txt.text = context;
    }

    protected void SetText(Transform trans, int num = 0){
        SetText(trans.GetComponent<Text>(), num.ToString());
    }

    protected void SetText(Transform trans, string context = ""){
        SetText(trans.GetComponent<Text>(), context);
    }

    protected void SetText(Text txt, int num = 0){
        SetText(txt, num.ToString());
    }

    protected void SetSprite(Image img, string path){
        Sprite sp = resSvc.LoadSprite(path, true);
        img.sprite = sp;
    }

    protected T GetOrAddComponent<T>(GameObject obj) where T : Component{
        T t = obj.GetComponent<T>();
        if (t == null){
            t = obj.AddComponent<T>();
        }

        return t;
    }

    protected Transform GetTrans(Transform trans, string name){
        if (trans != null){
            return trans.Find(name);
        }
        else{
            return transform.Find(name);
        }
    }

    #endregion

    #region Click event

    protected void OnClick(GameObject obj, Action<object> action, object args){
        PEListener listener = GetOrAddComponent<PEListener>(obj);
        listener.onClick = action;
        listener.args = args;
    }

    protected void OnClickDown(GameObject obj, Action<PointerEventData> action){
        PEListener listener = GetOrAddComponent<PEListener>(obj);
        listener.onClickDown = action;
    }

    protected void OnClickUp(GameObject obj, Action<PointerEventData> action){
        PEListener listener = GetOrAddComponent<PEListener>(obj);
        listener.onClickUp = action;
    }

    protected void OnDraw(GameObject obj, Action<PointerEventData> action){
        PEListener listener = GetOrAddComponent<PEListener>(obj);
        listener.onDraw = action;
    }

    #endregion
}